/*
* Copyright (c) Huawei Technologies Co., Ltd. 2020-2021. All rights reserved.
* gazelle is licensed under the Mulan PSL v2.
* You can use this software according to the terms and conditions of the Mulan PSL v2.
* You may obtain a copy of Mulan PSL v2 at:
*     http://license.coscl.org.cn/MulanPSL2
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
* PURPOSE.
* See the Mulan PSL v2 for more details.
*/

#ifndef __LSTACK_CONN_HTABLE_H__
#define __LSTACK_CONN_HTABLE_H__
#include <stdint.h>

#include <rte_rwlock.h>

#include <lwip/lwipgz_flow.h>
#include <lwip/lwipgz_hlist.h>

#include "common/gazelle_opt.h"

#define CONN_HTABLE_MAX_SIZE        2048

struct conn_htable_node {
    uint16_t queue_id;
    struct gazelle_quintuple quintuple;

    struct hlist_node conn_node;
};

struct conn_htable_hbucket {
    uint32_t chain_size;
    struct hlist_head chain;
};

struct conn_htable {
    rte_rwlock_t rwlock;
    uint32_t cur_conn_num;
    uint32_t max_conn_num;
    struct conn_htable_hbucket array[CONN_HTABLE_MAX_SIZE];
};

struct conn_htable *conn_htable_create(int max_conn_num);
int conn_htable_attach(void);
void conn_htable_destroy(void);

struct conn_htable_node *conn_htable_add_by_quintuple(struct gazelle_quintuple *quintuple, uint16_t queue_id);
int conn_htable_get_queue_by_quintuple(struct gazelle_quintuple *quintuple, uint16_t cur_queue_id);
void conn_htable_del_by_quintuple(struct gazelle_quintuple *quintuple);
void conn_htable_del_by_queue(uint16_t queue_id);
void conn_htable_dump(void);

#endif
